import Vue from 'vue';
import app from './app.vue';
import router from './common/router/router';
/****** util ******/
import util from './common/util';
Vue.prototype.$util = util;

import dicts from './common/dicts';
Vue.prototype.$dicts = dicts;

/****** util ******/
import echarts from 'echarts'
Vue.prototype.$echarts = echarts;
/**elementUI**/
import elementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
// import '@/assets/scss/variable.scss';
/**全局scss**/
// import './views/frame/theme.scss'
//引用样式
import '@/assets/scss/main.scss'
/**vue-easytable**/
import 'vue-easytable/libs/themes-base/index.css'
import {
  VTable,
  VPagination
} from 'vue-easytable';
Vue.component(VTable.name, VTable);
Vue.component(VPagination.name, VPagination);

Vue.prototype.$error = function(msg) {
    return elementUI.Message.error({
        message: msg,
        showClose: true,
        offset: 100,
        duration: 30000
    })
}

/******** axios配置 ***********/
import axios from 'axios';
Vue.prototype.$axios = axios;
// axios.defaults.withCredentials = true;
//axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.interceptors.request.use(
  function (config) {
    try {
      // console.log(config);
      // Do something before request is sent
    } catch (e) {
      Vue.$error(e);
    } finally {
      return config;
    }

  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
axios.interceptors.response.use(
  function (response) {
    // Do something with response data
    return response;
    if (response.data.result == 'true') {
      if (util.isJsonObj(response.data.resultData)) {
        response.data.resultData = util.num2str(response.data.resultData);
        return response;
      } else if (util.isArray(response.data.resultData)) {
        response.data.resultData = util.arrayValue2str(response.data.resultData);
        return response;
      } else {
        elementUI.Message.error('resultData is not a json obj or a Array');
        return response;
      }
    } else {
      elementUI.Message.error('response.data.result is not true');
      return response;
    }
  },
  function (error) {
    // Do something with response error
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          error.message = '请求错误';
          break;

        case 401:
          error.message = '未授权，请登录';
          break;

        case 403:
          error.message = '拒绝访问';
          break;

        case 404:
          error.message = '请求地址出错';
          break;

        case 405:
          error.message = '访问被拒绝';
          break;

        case 408:
          error.message = '请求超时';
          break;

        case 500:
          error.message = '服务器内部错误';
          break;

        case 501:
          error.message = '服务未实现';
          break;

        case 502:
          error.message = '网关错误';
          break;

        case 503:
          error.message = '服务不可用';
          break;

        case 504:
          error.message = '网关超时';
          break;

        case 505:
          error.message = 'HTTP版本不受支持';
          break;

        default:
      }
    }
    // 此处使用element UI 的提示组件
    const ms =
      error.message +
      '  ' +
      error.response.config.method +
      ' method : ' +
      error.response.status;
    Vue.$error(ms);
    return Promise.reject(error); // 返回接口返回的错误信息
  }
);

/******** http url配置 ***********/
import httpRoot from 'static/js/config'
Vue.prototype.httpRoot = httpRoot;



Vue.config.productionTip = false;
Vue.use(elementUI, {
  size: 'small'
});

/**************** vuex配置 ******************/
import store from './common/store';
//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
  //新增单点登录判断
  // let loginsid = to.query.loginsid;
  // if (to.path == '/home') {
  //   let userinfo = util.getLoginer() ;
  //   if (userinfo && userinfo.loginsid != "" && (loginsid==null || userinfo.loginsid == loginsid  ) ) {
  //     next(); 
  //   } else if (!userinfo || userinfo.loginsid == null || userinfo.loginsid == "" || userinfo.loginsid == undefined) {
  //     //如果localstorage中没有缓存的登录信息，再检查url中的参数中是否有登录的loginsid 
  //     if (loginsid) {
  //       //如果有则请求后台检查登录状态
  //       axios.post(httpRoot.BASEURL_UIM + 'rest/uim/frame/frmuser/chkloginer', {
  //         loginsid
  //       }).then(res => {
  //         if (res.data.result == "true") {  
  //           store.dispatch("user/cacheLoginer", res.data.resultData)
  //             .then(() => {
  //               next();
  //             })
  //             .catch(() => { });
  //         } else {
  //           next('/login');
  //         }
  //       }).catch(err => { })
  //     } else {
  //       //没有则跳转到登录页去登录
  //       next('/login');
  //     }
  //   }
  // } else {
  //   next();
  // }
  //  
  const role = store.getters['user/name'];
  if (!role && to.path !== '/login') {
    next('/login');
  } else {
    next();
  }
});

/*************** dialog *************/
import dialog from 'components/dialog';
Vue.prototype.$dialog = dialog;

/*************** form validate *************/
// import * as validate from 'common/validate';
// Vue.prototype.$validate = validate;


/*************** bus *************/
import bus from 'static/js/bus'
Vue.prototype.bus = bus;
Vue.prototype.tableListHeight = window.innerHeight - 60 - 40 - 25 - 36 - 45 - 10;

/*************** v-selectpage *************/
import vSelectPage from 'v-selectpage'
Vue.use(vSelectPage, {
  dataLoad: function(vue, data, params){
    data = data.split('/?')[0]+'/';
    return new Promise((resolve, reject)=>{
      axios.get(data, {
        params: params
      }).then(resp => resolve(resp), resp => reject(resp))
    })
  }
})

new Vue({
  router,
  store,
  render: h => h(app),
}).$mount('#app');

export {
  Vue,
  axios,
  store
};